package com.tusuo.stds.ui.level;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelRecordList;
import com.tusuo.stds.util.Utils;

import java.util.List;

/**
 * @author J.Huang
 * @version 1.0
 *
 */
class LevelRecordListAdapter extends BaseAdapter {

    private List<LevelRecordList.LevelRecord> dataList;
    private LayoutInflater layoutInflater;
    private Context context;

    LevelRecordListAdapter(Context context, List<LevelRecordList.LevelRecord> dataList) {
        this.context = context;
        this.dataList = dataList;
        this.layoutInflater = LayoutInflater.from(context);
    }

    private final class Component {
        TextView tvTitle;
        TextView tvCreatedTime;
        TextView tvCorrectRate;
        TextView tvClearanceTime;
        ImageView ivStar1;
        ImageView ivStar2;
        ImageView ivStar3;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public LevelRecordList.LevelRecord getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Component component = null;
        if (convertView == null) {
            component = new Component();
            // 获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.level_record_list_item, null);

            component.tvTitle = (TextView) convertView.findViewById(R.id.level_record_list_item_tv_title);
            component.tvCreatedTime = (TextView) convertView.findViewById(R.id.level_record_list_item_tv_created_time);
            component.tvCorrectRate = (TextView) convertView.findViewById(R.id.level_record_list_item_tv_correct_rate);
            component.tvClearanceTime = (TextView) convertView.findViewById(R.id.level_record_list_item_tv_clearance_time);
            component.ivStar1 = (ImageView) convertView.findViewById(R.id.level_record_list_item_iv_star_1);
            component.ivStar2 = (ImageView) convertView.findViewById(R.id.level_record_list_item_iv_star_2);
            component.ivStar3 = (ImageView) convertView.findViewById(R.id.level_record_list_item_iv_star_3);

            convertView.setTag(component);
        } else {
            component = (Component) convertView.getTag();
        }

        LevelRecordList.LevelRecord levelRecord = dataList.get(position);

        // 绑定数据

        component.tvTitle.setText(levelRecord.Title);
        component.tvCreatedTime.setText(context.getString(R.string.level_record_created_time, levelRecord.getFormattedCreateTime()));

        switch (levelRecord.CompletionStatus) {
            case 0:
                component.ivStar1.setImageResource(R.drawable.ic_star_unselected);
                component.ivStar2.setImageResource(R.drawable.ic_star_unselected);
                component.ivStar3.setImageResource(R.drawable.ic_star_unselected);
                break;
            case 10:
                component.ivStar1.setImageResource(R.drawable.ic_star_selected);
                component.ivStar2.setImageResource(R.drawable.ic_star_unselected);
                component.ivStar3.setImageResource(R.drawable.ic_star_unselected);
                break;
            case 20:
                component.ivStar1.setImageResource(R.drawable.ic_star_selected);
                component.ivStar2.setImageResource(R.drawable.ic_star_selected);
                component.ivStar3.setImageResource(R.drawable.ic_star_unselected);
                break;
            case 30:
                component.ivStar1.setImageResource(R.drawable.ic_star_selected);
                component.ivStar2.setImageResource(R.drawable.ic_star_selected);
                component.ivStar3.setImageResource(R.drawable.ic_star_selected);
                break;
            case 100:
            default:
                component.tvCreatedTime.setText(R.string.level_status_not_pass);
                component.ivStar1.setImageResource(R.drawable.ic_star_unselected);
                component.ivStar2.setImageResource(R.drawable.ic_star_unselected);
                component.ivStar3.setImageResource(R.drawable.ic_star_unselected);
                break;
        }

        component.tvCorrectRate.setText(context.getString(R.string.level_record_correct_rate, (int) (levelRecord.CorrectRate * 100)));
        component.tvClearanceTime.setText(context.getString(R.string.level_record_clearance_time, Utils.secondsToHumanReadable(levelRecord.ClearanceTime)));

        return convertView;
    }
}
